﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ChinaColorDisplay
{
    public static class Utility
    {
        /// <summary>
        /// 获取嵌入的资源
        /// </summary>
        /// <param name="assembly">资源所在程序集</param>
        /// <param name="file">资源名称 EndsWith 匹配</param>
        /// <returns>资源流</returns>
        public static Stream GetEmbedResourceStream(Assembly assembly, string file)
        {
            string realFile = assembly.GetManifestResourceNames().FirstOrDefault(p => p.EndsWith(file));
            if (!string.IsNullOrEmpty(realFile))
            {
                return assembly.GetManifestResourceStream(realFile);
            }

            return null;
        }

        /// <summary>
        /// 获取嵌入的文本资源
        /// </summary>
        /// <param name="assembly">资源所在的程序集</param>
        /// <param name="file">资源名称 EndsWith 匹配</param>
        /// <returns>文本内容</returns>
        public static string GetEmbedTextResource(Assembly assembly, string file)
        {
            Stream stream = GetEmbedResourceStream(assembly, file);
            if (stream != null)
            {
                TextReader textReader = new StreamReader(stream);
                return textReader.ReadToEnd();
            }

            return "";
        }
    }
}
